package com.feonix.common.util.jdbc;

public class OracleSeq {

	/**
	 * 创建自增序列
	 * 
	 * @param seq
	 * @return
	 */
	public static String createSeq(String seq) {
		String str = "CREATE SEQUENCE \"" + seq.toUpperCase().trim() + "_SEQ\" MINVALUE 1 "
				+ "MAXVALUE 9999999999999999999999999999 " + "INCREMENT BY 1 START WITH 1 CACHE 20" + " ORDER NOCYCLE";
		return str;
	}

	/**
	 * 创建自增触发器
	 * 
	 * @param seq
	 * @return
	 */
	public static String createTrigger(String seq) {
		StringBuffer stringBuffer = new StringBuffer(0);
		stringBuffer.append("CREATE OR REPLACE TRIGGER \"" + seq.toUpperCase() + "_SETID\"");
		stringBuffer.append("BEFORE INSERT ON \"" + seq.toUpperCase() + "\" FOR EACH ROW\n");
		stringBuffer.append("DECLARE\n");
		stringBuffer.append("next_id NUMBER;\n");
		stringBuffer.append("BEGIN\n");
		stringBuffer.append("SELECT " + seq.toUpperCase() + "_SEQ.NEXTVAL\n");
		stringBuffer.append("INTO next_id\n");
		stringBuffer.append("FROM dual;\n");
		stringBuffer.append(":new.iid:= next_id;\n");
		stringBuffer.append(" END;\n");
		String str = stringBuffer.toString();
		stringBuffer.delete(0, stringBuffer.length());
		return str;
	}

	public static String enableSeq(String seq) {
		StringBuffer sb = new StringBuffer(0);
		sb.append("ALTER TRIGGER \"" + seq.toUpperCase() + "_SETID\" ENABLE\n");
		String str = sb.toString();
		sb.delete(0, sb.length());
		return str;
	}

	public static String removeSeq(String seq) {
		return "DROP TRIGGER " + seq.toUpperCase() + "_SETID";
	}

	public static String disableSeq(String seq) {
		return "ALTER TRIGGER " + seq.toUpperCase() + "_SETID DISABLE";
	}

}
